package cn.hotel.servlet;

import cn.hotel.pojo.ClientInformationManagement;
import cn.hotel.pojo.HouseType;
import cn.hotel.pojo.OrderManagement;
import cn.hotel.service.FrontEndService;
import cn.hotel.service.FrontEndServiceImpl;
import cn.hotel.service.OrderFormService;
import cn.hotel.service.OrderFormServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/FrontEnd")
public class FrontEndServlet extends HttpServlet {
    private FrontEndService frontEndService = new FrontEndServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out  = response.getWriter();
        FrontEndService frontEndService = new FrontEndServiceImpl();
        String type = request.getParameter("type");
        type = type == null ? "register" : type;
        switch (type){
            //前端注册
            case "register" :
                register(request,response,out, frontEndService);
                break;
                //成为会员
            case "synchronization" :
                break;
            //前端展示房型
            case "show" :
                show(request,response,out, frontEndService);
                break;
            //前端登录
            case "checkName" :
                checkName(request,request,out,frontEndService);
                break;
                //前端根据房型编号显示房型
            case "houseType" :
                houseType(request,response,out,frontEndService);
                break;
                //前端预约
            case "reserve" :
                reserve(request,response,out,frontEndService);
                break;
        }
    }

    //前端预约
    private void reserve(HttpServletRequest request, HttpServletResponse response, PrintWriter out, FrontEndService frontEndService) throws ServletException,IOException{
        OrderManagement orderManagement = new OrderManagement();
        OrderFormService orderFormService = new OrderFormServiceImpl();
        String password = request.getParameter("password");
        orderManagement.setPassword(password);
        String manageName = request.getParameter("manageName");
        orderManagement.setManageName(manageName);
        String checkInDate = request.getParameter("checkInDate");
        orderManagement.setCheckInDate(checkInDate);
        String checkOutDate = request.getParameter("checkOutDate");
        orderManagement.setCheckOutDate(checkOutDate);
        String mobilePhone = request.getParameter("mobilePhone");
        orderManagement.setMobilePhone(mobilePhone);
        String identityCard = request.getParameter("identityCard");
        orderManagement.setIdentityCard(identityCard);
        int personNum = 0;
        try {
            personNum = Integer.parseInt(request.getParameter("personNum"));
        }catch (Exception e){}
        orderManagement.setPersonNum(personNum);
        int houseTypeId = 0;
        try {
            houseTypeId = Integer.parseInt(request.getParameter("houseTypeId"));
        }catch (Exception e){}
        orderManagement.setHouseTypeId(houseTypeId);
        String price = request.getParameter("price");
        orderManagement.setPrice(price);
        int roomNumber = 0;
        try {
            roomNumber = Integer.parseInt(request.getParameter("roomNumber"));
        }catch (Exception e){}
        orderManagement.setRoomNumber(roomNumber);
        String remark = request.getParameter("remark");
        orderManagement.setRemark(remark);
        int reserve = frontEndService.reserve(orderManagement);
        if (reserve == 0){
            out.print("<script>alert('预约失败!');location.href='"+request.getContextPath()+"/FrontEnd?type=show';</script>");
        }else {
            int setState = orderFormService.setState(2,roomNumber);
            out.print("<script>alert('预约成功!');location.href='"+request.getContextPath()+"/FrontEnd?type=show';</script>");
        }
    }

    //查看房型
    private void houseType(HttpServletRequest request, HttpServletResponse response, PrintWriter out, FrontEndService frontEndService) throws ServletException,IOException{
        Long id = 0l;
        try {
            id = new Long(request.getParameter("id"));
        }catch (Exception e){}
        HouseType display = frontEndService.display(id);
        request.setAttribute("display",display);
        request.getRequestDispatcher("frontEndjsp/reserve.jsp").forward(request,response);
    }

    //前端登录验证
    private void checkName(HttpServletRequest request, HttpServletRequest request1, PrintWriter out, FrontEndService frontEndService) throws ServletException,IOException{
        String mobilePhone = request.getParameter("mobilePhone");
        String password = request.getParameter("password");
        ClientInformationManagement cim = frontEndService.checkName(mobilePhone,password);
        if (cim != null){
            request.getSession().setAttribute("cim",cim);

            out.print("<script>location.href='"+request.getContextPath()+"/FrontEnd?type=show';</script>");
        }else {
            out.print("<script>alert('登录失败!');location.href='"+request.getContextPath()+"/frontEndjsp/logins.jsp';</script>");
        }
    }

    //前端展示房型
    private void show(HttpServletRequest request, HttpServletResponse response, PrintWriter out, FrontEndService frontEndService) throws ServletException,IOException {
        int count1 = frontEndService.count(1);
        int count2 = frontEndService.count(2);
        int count3 = frontEndService.count(3);
        int count4 = frontEndService.count(4);
        List<HouseType> houseType = frontEndService.show();
        request.getSession().setAttribute("count1",count1);
        request.getSession().setAttribute("count2",count2);
        request.getSession().setAttribute("count3",count3);
        request.getSession().setAttribute("count4",count4);
        request.getSession().setAttribute("houseType",houseType);
        request.getRequestDispatcher("frontEndjsp/show.jsp").forward(request,response);
    }

    //前端注册
    private void register(HttpServletRequest request, HttpServletResponse response, PrintWriter out, FrontEndService frontEndService) throws ServletException,IOException{
        ClientInformationManagement clientInformationManagement = new ClientInformationManagement();
        String manageName = request.getParameter("manageName");
        clientInformationManagement.setManageName(manageName);
        String password = request.getParameter("password");
        clientInformationManagement.setPassword(password);
        String mobilePhone = request.getParameter("mobilePhone");
        clientInformationManagement.setMobilePhone(mobilePhone);
        String identityCard = request.getParameter("identityCard");
        clientInformationManagement.setIdentityCard(identityCard);
        String sex = request.getParameter("sex");
        clientInformationManagement.setSex(sex);
        int registers = frontEndService.register(clientInformationManagement);
        if (registers == 1){
            out.print("<script>location.href='"+request.getContextPath()+"/frontEndjsp/logins.jsp';</script>");
        }else {
            out.print("<script>alert('删除失败!');location.href='"+request.getContextPath()+"/frontEndjsp/logins.jsp';</script>");
        }
    }


}
